package com.dmap.knowledge.service.impl;

import com.dmap.knowledge.dao.ArticleColumnMapper;
import com.dmap.knowledge.po.ArticleColumn;
import com.dmap.knowledge.po.ArticleColumnExample;
import com.dmap.knowledge.po.Scene;
import com.dmap.knowledge.service.*;
import com.dmap.knowledge.vo.ArticleColumnVo;
import com.dmap.knowledge.vo.ArticleInfoVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zmh
 * @date 18-7-18 下午7:08
 */
@Service
public class ArticleColumnExtensionServiceImpl implements ArticleColumnExtensionService {
    @Resource
    private ArticleColumnMapper articleColumnMapper;
    @Resource
    private SceneService sceneService;
    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleExtensionService articleExtensionService;
    @Resource
    private ArticleColumnAppExtensionService articleColumnAppExtensionService;
    
    @Override
    public List<ArticleColumn> selectArticleColumnDownList(String lang) {
        List<ArticleColumn> articleColumnList;
        ArticleColumnExample articleColumnExample = new ArticleColumnExample();
        ArticleColumnExample.Criteria criteria = articleColumnExample.createCriteria();
        if (lang != null) {
            criteria.andLangEqualTo(lang);
        }
        //启用
        criteria.andStatusEqualTo(1);
        articleColumnList = articleColumnMapper.selectByExample(articleColumnExample);
        return articleColumnList;
    }
    
    @Override
    public int checkColumnList(String lang, String name, String key,String columnId) {
        int count;
        ArticleColumnExample articleColumnExample = new ArticleColumnExample();
        ArticleColumnExample.Criteria criteria = articleColumnExample.createCriteria();
        if (lang != null) {
            criteria.andLangEqualTo(lang);
        }
        if (name != null) {
            criteria.andNameEqualTo(name);
        }
        if (key != null) {
            criteria.andKeyNameEqualTo(key);
        }
        if(columnId!= null){
            criteria.andIdNotEqualTo(Long.parseLong(columnId));
        }
        count = articleColumnMapper.countByExample(articleColumnExample);
        return count;
    }
    
    @Override
    public ArticleColumn selectByArticleColumnApp(long columnId, String key) {
        ArticleColumnExample articleColumnExample = new ArticleColumnExample();
        ArticleColumnExample.Criteria criteria = articleColumnExample.createCriteria();
        if (columnId != 0) {
            criteria.andIdEqualTo(columnId);
        }
        if (key != null) {
            criteria.andKeyNameLike("%"+key+"%");
        }
        List<ArticleColumn> articleColumnList = articleColumnMapper.selectByExample(articleColumnExample);
        if(CollectionUtils.isNotEmpty(articleColumnList)){
            return articleColumnList.get(0);
        }
        return null;
    }
    
    @Override
    public ArticleInfoVo selectArticleColumnList(String lang, String sceneId, String key, long order, int pageIndex, int pageSize) {
        ArticleColumnExample articleColumnExample = new ArticleColumnExample();
        articleColumnExample.setLimitStart((pageIndex - 1) * pageSize);
        articleColumnExample.setLimitEnd(pageSize);
        articleColumnExample.setOrderByClause("create_time desc");
        ArticleColumnExample.Criteria criteria = articleColumnExample.createCriteria();
        if (lang != null) {
            criteria.andLangEqualTo(lang);
        }
        if (sceneId != null) {
            criteria.andSceneIdEqualTo(Long.parseLong(sceneId));
        }
        if (key != null) {
            criteria.andKeyNameLike("%" + key + "%");
        }

        List<ArticleColumnVo> articleColumnVoList = new ArrayList<>();
        List<ArticleColumn> articleColumns = articleColumnMapper.selectByExample(articleColumnExample);
        articleColumns.forEach(articleColumn -> {
            int count = articleExtensionService.countArticleByColumn(null,articleColumn.getId());
            Scene scene = sceneService.selectByColumnId(articleColumn.getSceneId());
            if (scene == null) {
                scene = new Scene();
                scene.setId(articleColumn.getSceneId());
                scene.setName("不存在该场景");
            }
            ArticleColumnVo articleColumnVo = new ArticleColumnVo(articleColumn, scene);
            //查询app的关联数
            articleColumnVo.setAppColumnNumber(articleColumnAppExtensionService.countArticleColumnAppByColumnId(articleColumn.getId()));
            articleColumnVo.setRelevanceNumber(count);
            articleColumnVoList.add(articleColumnVo);
        });
        ArticleInfoVo articleInfoVo = new ArticleInfoVo();
        articleInfoVo.setCount(countArticleColumnList(lang, sceneId, key, order));
        articleInfoVo.setColumnList(articleColumnVoList);
        return articleInfoVo;
    }
    
    @Override
    public int countArticleColumn() {
        //所有的articlecolumn的数量
        ArticleColumnExample articleColumnExample = new ArticleColumnExample();
        return articleColumnMapper.countByExample(articleColumnExample);
    }
    
    @Override
    public int countArticleColumnList(String lang, String sceneId, String key, long order) {
        ArticleColumnExample articleColumnExample = new ArticleColumnExample();
        ArticleColumnExample.Criteria criteria = articleColumnExample.createCriteria();
        if (lang != null) {
            criteria.andLangEqualTo(lang);
        }
        if (sceneId != null) {
            criteria.andSceneIdEqualTo(Long.parseLong(sceneId));
        }
        if (key != null) {
            criteria.andKeyNameLike("%" + key + "%");
        }
        return articleColumnMapper.countByExample(articleColumnExample);
    }
}
